package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.sell.dao.CustomerComplaintLabelCategoryDao;
import com.wing.sell.model.entity.CustomerComplaintLabelCategory;
import com.wing.sell.model.entity.CustomerComplaintLabelCategory;
import com.wing.sell.model.request.CustomerComplaintLabelCategoryForm;
import com.wing.sell.model.response.CustomerComplaintLabelCategoryVO;
import com.wing.sell.model.response.CustomerComplaintLabelCategoryVO;
import com.wing.sell.model.response.NodeDataVO;
import com.wing.sell.service.CustomerComplaintLabelCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客诉标签分类-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CustomerComplaintLabelCategoryServiceImpl extends ServiceImpl<CustomerComplaintLabelCategoryDao, CustomerComplaintLabelCategory> implements CustomerComplaintLabelCategoryService {

    @Override
    public int add(CustomerComplaintLabelCategoryForm customerComplaintLabelCategoryForm) {

        //校验

        CustomerComplaintLabelCategory customerComplaintLabelCategory =new CustomerComplaintLabelCategory();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CustomerComplaintLabelCategory.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(customerComplaintLabelCategoryForm, customerComplaintLabelCategory, copyOptions);

        return baseMapper.insert(customerComplaintLabelCategory);
    }

    @Override
    public List<CustomerComplaintLabelCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CustomerComplaintLabelCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<CustomerComplaintLabelCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CustomerComplaintLabelCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public JsonResult getTreeData(Long selectId) {

        Map<String, Object> params = new HashMap<>();
        params.put("isDeleted", 1);
        List<CustomerComplaintLabelCategoryVO> list=baseMapper.list4app(params);
        Map<Long,List<NodeDataVO>> map=new HashMap<>();
        for (CustomerComplaintLabelCategoryVO customerComplaintLabelCategoryVO:list) {
            if (map.containsKey(customerComplaintLabelCategoryVO.getParentId())){
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(customerComplaintLabelCategoryVO.getId().toString());
                nodeDataVO.setName(customerComplaintLabelCategoryVO.getName());
                if(!customerComplaintLabelCategoryVO.getIsFinal() || !customerComplaintLabelCategoryVO.getIsDeleted()){
                    nodeDataVO.setDisabled(true);
                }
                if(selectId != null && selectId.equals(customerComplaintLabelCategoryVO.getId())){
                    nodeDataVO.setSelected(true);
                }
                map.get(customerComplaintLabelCategoryVO.getParentId()).add(nodeDataVO);
            }else{
                List<NodeDataVO> tmpList=new ArrayList<>();
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(customerComplaintLabelCategoryVO.getId().toString());
                nodeDataVO.setName(customerComplaintLabelCategoryVO.getName());
                if(!customerComplaintLabelCategoryVO.getIsFinal() || !customerComplaintLabelCategoryVO.getIsDeleted()){
                    nodeDataVO.setDisabled(true);
                }
                if(selectId != null && selectId.equals(customerComplaintLabelCategoryVO.getId())){
                    nodeDataVO.setSelected(true);
                }
                tmpList.add(nodeDataVO);
                map.put(customerComplaintLabelCategoryVO.getParentId(),tmpList);
            }
        }
        List<NodeDataVO> rootList=map.get(-1L);
        if (rootList==null){
            return JsonResult.success();
        }
        constructTree(rootList,map);
        return JsonResult.success(rootList);
    }

    @Override
    public boolean delete(Long customerComplaintLabelCategoryId) {
        int count = baseMapper.selectCount(new QueryWrapper<CustomerComplaintLabelCategory>().eq("parent_id", customerComplaintLabelCategoryId));
        if(count > 0){
            throw new BusinessException(500, "请先删除子分类");
        }
        return removeById(customerComplaintLabelCategoryId);
    }

    @Override
    public int enable(Long id) {
        int i = 0;
        CustomerComplaintLabelCategory customerComplaintLabelCategory = baseMapper.selectById(id);
        if(customerComplaintLabelCategory != null){
            customerComplaintLabelCategory.setIsDeleted(true);
            i = baseMapper.updateById(customerComplaintLabelCategory);
        }
        return i;
    }

    @Override
    public int disable(Long id) {
        int i = 0;
        CustomerComplaintLabelCategory customerComplaintLabelCategory = baseMapper.selectById(id);
        if(customerComplaintLabelCategory != null){
            customerComplaintLabelCategory.setIsDeleted(false);
            i = baseMapper.updateById(customerComplaintLabelCategory);
        }
        return i;
    }

    private void  constructTree(List<NodeDataVO> parentList,Map<Long,List<NodeDataVO>> dataMap){
        for (NodeDataVO node:parentList) {
            Long nodeId=Long.valueOf(node.getValue());
            List<NodeDataVO> childList=dataMap.get(nodeId);
            if (childList!=null){
                node.setChildren(childList);
                constructTree(childList,dataMap);
            }
        }
    }
    
}
